package com.itany.corejava.code11_线程.zuoye;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author MissChen
 * @date 2024年03月12日 16:51
 * 创建一个类，实现Runnable接口
 * 在该类中有一个数组如：int[] arr={10,5,20,50,100,200,500,800,2,80,300};
 * 定义两个线程，借助于上面创建的类
 * 在控制台每隔一秒随机输出上面数组中的任意一个值，不允许重复，输出完就结束
 *
 */
public class Test01 {
    public static void main(String[] args) {
        MyThread myThread=new MyThread();
        Thread t1=new Thread(myThread,"抽奖箱一");
        Thread t2=new Thread(myThread,"抽奖箱二");
        t1.start();
        t2.start();
    }
}

class MyThread implements Runnable{

    int[] arr={10,5,20,50,100,200,500,800,2,80,300};
    List<Integer> list=new ArrayList<>();
    @Override
    public void run() {
        while(true){
            synchronized (this){
                Random r=new Random();
                int i=r.nextInt(arr.length);//[0-9]
                if(!list.contains(arr[i])){
                    list.add(arr[i]);
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"抽了一个数字:"+arr[i]);
                }
            }
            if(list.size()==arr.length){
                break;
            }
        }
    }
}
